package com.geek.novel.dialog

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.geek.novel.R
import com.geek.novel.activity.book.ViewerActivity
import com.geek.novel.adapter.BaseRecyclerViewAdapter
import com.geek.novel.adapter.BookSectionIndexAdapter
import com.geek.novel.entity.BookRead
import com.geek.novel.ui.ReadView
import com.geek.novel.ui.widget.FastScroller
import com.zackratos.ultimatebarx.ultimatebarx.addStatusBarTopPadding


/**
 * 左边的章节列表的弹框
 */
class SectionIndexDialog(private val mContext: ViewerActivity, private val sectionView: ReadView): CustomDialog(mContext) {

    /**列表容器*/
    private lateinit var sectionIndexPanel: ViewGroup

    /**列表*/
    private lateinit var sectionIndexView: RecyclerView

    /**列表适配器*/
    private lateinit var sectionIndexAdapter: BookSectionIndexAdapter

    /**章节总数显示文本*/
    private lateinit var tvDialogSectionCount: TextView

    /**章节排序*/
    private lateinit var tvSectionIndexSort: TextView

    /**初始化容器控件*/
    @SuppressLint("InflateParams")
    override fun initDialogViews(parent: ViewGroup): List<View> {
        val views = ArrayList<View>()

        //加载弹出框的布局
        sectionIndexPanel = LayoutInflater.from(this.context).inflate(R.layout.dialog_section_index, null) as ViewGroup
        val lp2 = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT)
        parent.addView(sectionIndexPanel, lp2)
        views.add(sectionIndexPanel)

        return views
    }

    override fun initDialog() {
        //添加状态栏的高度
        getView(R.id.view_block_offset_top).addStatusBarTopPadding()

        //获取书本
        val book = this.mContext.book

        //初始化章节列表
        this.initListView()

        //初始化书本信息
        this.initBookInfo(book)

    }

    /**初始化书本信息*/
    private fun initBookInfo(book: BookRead?){
        //显示书名
        val tvBookName = getView(R.id.tv_dialog_book_index_book_name) as TextView
        tvBookName.text = book?.bookName

        //显示章节总数
        this.tvDialogSectionCount = getView(R.id.tv_dialog_section_count) as TextView

        //章节排序
        tvSectionIndexSort = getView(R.id.tv_dialog_section_index_sort) as TextView
        tvSectionIndexSort.setOnClickListener(this)
        this.initSort()

    }

    /**初始化排序信息*/
    private fun initSort(){
        val sortDesc = sectionIndexAdapter.getSortDesc()
        tvSectionIndexSort.text = if(sortDesc) "倒序" else "正序"
    }

    /**初始化章节列表*/
    @SuppressLint("VisibleForTests")
    private fun initListView(){
        //添加适配器
        sectionIndexAdapter = BookSectionIndexAdapter(mContext, mContext.bookIndexList)

        //绑定ListView和适配器
        sectionIndexView = getView(R.id.lv_section_index) as RecyclerView
        sectionIndexView.layoutManager = LinearLayoutManager(context)
        sectionIndexView.adapter = sectionIndexAdapter


        val resources = context.resources
        val verticalThumbDrawable = context.getDrawable(R.drawable.recycle_view_vertical_scrollbar) as StateListDrawable
        val verticalTrackDrawable: Drawable = context.getDrawable(R.drawable.recycle_view_vertical_scrollbar_bg)!!

        //给RecyclerView添加滚动条
        FastScroller(sectionIndexView, verticalThumbDrawable, verticalTrackDrawable,
            verticalThumbDrawable, verticalTrackDrawable,
            resources.getDimensionPixelSize(R.dimen.d_10),
            resources.getDimensionPixelSize(R.dimen.fastscroll_minimum_range),
            resources.getDimensionPixelOffset(R.dimen.fastscroll_margin),
            resources.getDimensionPixelSize(R.dimen.d_30))

        //绑定点击事件
        sectionIndexAdapter.onItemClickListener = object: BaseRecyclerViewAdapter.OnItemClickListener{
            override fun onItemClick(position: Int, event: MotionEvent?) {
                val bookIndex = mContext.bookIndexList[position]
                close()
                sectionView.loadBookSection(position, bookIndex)
            }
        }
    }

    /**滚动到当前章节所在的位置*/
    private fun gotoCurBookIndexPosition(){
        //滚动到位置
        if(this.sectionIndexAdapter.list.isNotEmpty() && mContext.book != null){
            val layoutManager = this.sectionIndexView.layoutManager as LinearLayoutManager
            val position = mContext.book!!.curReadIndex
            val index = this.sectionIndexAdapter.getIndex(position)
            layoutManager.scrollToPositionWithOffset(index, 0)
        }
        sectionIndexAdapter.notifyDataSetChanged()
    }

    private fun getView(id: Int): View{
        return sectionIndexPanel.findViewById(id)
    }

    /**刷新章节列表内容*/
    @SuppressLint("SetTextI18n")
    fun refreshSectionList(){
        tvDialogSectionCount.text = "共${this.sectionIndexAdapter.list.size}章"
        gotoCurBookIndexPosition()
    }

    /**绑定点击事件的回调*/
    override fun clickEvent(id: Int, v: View) {
        when(id){
            //排序事件
            R.id.tv_dialog_section_index_sort->{
                sectionIndexAdapter.setSortDesc(!sectionIndexAdapter.getSortDesc())
                this.initSort()
                gotoCurBookIndexPosition()
            }
        }
    }

    override fun open() {
        this.sectionView.onHide()
        //刷新列表(主要是为了渲染列表的高亮)
        this.sectionIndexAdapter.notifyDataSetChanged()
        this.gotoCurBookIndexPosition()
        this.openDialogAnim(arrayListOf(R.anim.dialog_section_index_in))
    }

    override fun close() {
        this.sectionView.onShow()
        this.closeDialogAnim(arrayListOf(R.anim.dialog_section_index_out))
    }

}